/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChartExportOptionsInternalFrame.java
 *
 * Created on 16.06.2010, 08:18:32
 */

package GUI.iFrames;

import java.util.Vector;

import javax.swing.JFileChooser;

import org.jfree.chart.plot.PlotOrientation;

import GUI.BaseFrame;
import GUI.iFrames.DataSourceComponents.DataSourcesInternalFrame;

/**
 *
 * @author Bunkowski
 */
public class ChartExportOptionsInternalFrame extends javax.swing.JInternalFrame {

	public static final long serialVersionUID = 1L;
	
	private DataSourcesInternalFrame dsif;

    /** Creates new form ChartExportOptionsInternalFrame */
    public ChartExportOptionsInternalFrame(DataSourcesInternalFrame dsif) {
        initComponents();
        this.dsif = dsif;
        jComboBox1.addItem(BaseFrame.getSelectedFileTreeFile());
        jComboBox1.setSelectedItem(BaseFrame.getSelectedFileTreeFile());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        barchartButton = new javax.swing.JRadioButton();
        boxplotButton = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        horizontalButton = new javax.swing.JRadioButton();
        verticalButton = new javax.swing.JRadioButton();
        saveButton = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        chooseButton = new javax.swing.JButton();

        setClosable(true);
        setResizable(true);
        setTitle("Chart export options");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Type"));

        buttonGroup1.add(barchartButton);
        barchartButton.setSelected(true);
        barchartButton.setText("Barchart");

        buttonGroup1.add(boxplotButton);
        boxplotButton.setText("Boxplot");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(barchartButton)
            .addComponent(boxplotButton)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(barchartButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boxplotButton)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Orientation"));

        buttonGroup2.add(horizontalButton);
        horizontalButton.setSelected(true);
        horizontalButton.setText("Horizontal");

        buttonGroup2.add(verticalButton);
        verticalButton.setText("Vertical");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(horizontalButton))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(verticalButton)
                .addContainerGap(12, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(horizontalButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(verticalButton)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        jComboBox1.setEditable(true);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { " " }));
        jComboBox1.setAutoscrolls(true);

        chooseButton.setText("Folder:");
        chooseButton.setMargin(new java.awt.Insets(2, 4, 2, 4));
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(chooseButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, 0, 319, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 131, Short.MAX_VALUE)
                        .addComponent(saveButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chooseButton)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(saveButton)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseButtonActionPerformed
        JFileChooser chooser = new JFileChooser(BaseFrame.getSelectedFileTreeFile());
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        chooser.setDialogTitle("Choose folder for charts");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String folder = chooser.getSelectedFile().toString();
            jComboBox1.addItem(folder);
            jComboBox1.setSelectedItem(folder);
        }
    }//GEN-LAST:event_chooseButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
            String folder = jComboBox1.getSelectedItem().toString();
            Vector<String> xAxisLabels = dsif.getSelectedLabelColumn();
            Vector<String> classes = dsif.getSelectedClassColumn();
            int xshift = 0;
            int yshift = 0;
            for (int i = 0; i < dsif.table.getSelectedColumns().length; i++) {
                int cindx = dsif.table.getSelectedColumns()[i];
                Vector<Double> data = new Vector<Double>();
                for (int j = 0; j < dsif.table.getRowCount(); j++) {
                    data.add(Double.parseDouble((String) dsif.table.getValueAt(j, cindx)));
                }
                ChartInternalFrame cif = new ChartInternalFrame(dsif.getSubstanceAt(cindx).name, data, classes, xAxisLabels);
                cif.setBounds(xshift += 20, yshift += 20, 600, 400);
                if(horizontalButton.isSelected()){
                    cif.plotorientation = PlotOrientation.HORIZONTAL;
                }
                if(verticalButton.isSelected()){
                    cif.plotorientation = PlotOrientation.VERTICAL;
                }

                if(barchartButton.isSelected()){
                //barchart is default
                }
                if(boxplotButton.isSelected()){
                cif.showAsBoxChart();
                }
                cif.saveToFolder(folder);
                cif.dispose();
            }
            this.dispose();
}//GEN-LAST:event_saveButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton barchartButton;
    private javax.swing.JRadioButton boxplotButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton chooseButton;
    private javax.swing.JRadioButton horizontalButton;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton saveButton;
    private javax.swing.JRadioButton verticalButton;
    // End of variables declaration//GEN-END:variables

}
